/*
Filename:    association_collection.cpp
Author:      Erik Öjebo
Date:        2009-01-10
Description: A collection of SCTP association id:s.
             Adds new id:s and removes old id:s by handling SCTP_ASSOC_CHANGE
             notifications.
*/

// See header for documentation

#include <netinet/sctp.h>
#include <iostream>
#include "association_collection.h"

void AssociationCollection::
    handleAssociationEvent(union sctp_notification* notification)
{
    struct sctp_assoc_change* assoc_change;

    assoc_change = (struct sctp_assoc_change*)notification;

    switch (assoc_change->sac_state)
    {
    case SCTP_COMM_UP:
        // A new association has been created, add it to the collection:
        associations.push_back(assoc_change->sac_assoc_id);
        break;

    case SCTP_COMM_LOST:
        // An association has been lost, remove it from the collection:
        associations.remove(assoc_change->sac_assoc_id);
        break;

    case SCTP_SHUTDOWN_COMP:
        // An association has been shut down, remove it from the collection:
        associations.remove(assoc_change->sac_assoc_id);
        break;

    default:
        break;
    }
}

AssociationCollectionConstIterator AssociationCollection::begin() const
{
    // Return iterator pointing at the first element of the collection:
    return AssociationCollectionConstIterator(this);
}

std::list<sctp_assoc_t>::const_iterator AssociationCollection::listBegin() const
{
    // Return iterator to the beginning of the internal list:
    return associations.begin();
}

std::list<sctp_assoc_t>::const_iterator AssociationCollection::listEnd() const
{
    // Return iterator to the end of the internal list:
    return associations.end();
}

int AssociationCollection::count() const
{
    // Return the number of associations in the collection:
    return associations.size();
}

void AssociationCollection::remove(sctp_assoc_t associationId)
{
    associations.remove(associationId);
}
